package com.zsrt.day20170413.pm.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.junit.Test;

/**
*@author jzh
*@创建时间 2017年4月13日下午8:00:10
*类说明：
*/
public class GetConstructorTest {
	
	@Test
	public void getConsructorTest() throws Exception{
		
		Class class1=Class.forName("com.zsrt.day20170413.pm.reflect.Person");
		System.out.println("除private外的构造方法+++++++++++++++++++++++++++");
		Constructor[] constructors = class1.getConstructors();
		for(Constructor c:constructors ){
			System.out.println(c);
		}
		System.out.println("全部的构造方法+++++++++++++++++++++++++++");
		Constructor[] declaredConstructors = class1.getDeclaredConstructors();
		for(Constructor c:declaredConstructors ){
			System.out.println(c);
		}
		System.out.println("无参的构造方法+++++++++++++++++++++++++++");
		Constructor declaredConstructor = class1.getDeclaredConstructor();
		Object object = declaredConstructor.newInstance();
		System.out.println(object instanceof Person);
		System.out.println("有参的构造方法+++++++++++++++++++++++++++");
		Constructor declaredConstructor1=class1.getDeclaredConstructor(int.class,String.class);
		Object object2 = declaredConstructor1.newInstance(110,"张三");
		System.out.println(object2);
		System.out.println("私有的构造方法+++++++++++++++++++++++++++");
		Constructor declaredConstructor2=class1.getDeclaredConstructor(int.class);
		declaredConstructor2.setAccessible(true);
		Object object3 = declaredConstructor2.newInstance(100);
		System.out.println(object3);

		
		
	}

}
